package sites.manager.admin.role;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import sites.manager.common.kit.Ret;
import sites.manager.common.model.Account;
import sites.manager.common.model.Role;

/**
 * 角色管理
 */
public class RoleController extends Controller {

    @Inject
    RoleService srv;

    public void enterMng(){
        render("pageRoleMng.html");
    }

    public void getRoles(){
        String name = get("name", "");
        int page = getInt("page", 1);
        int limit = getInt("limit", 10);

        Ret ret = srv.getRoles(name, page, limit);
        renderJson(ret);
    }

    public void enterAdd(){
        render("respRoleAdd.html");
    }

    @Before(RoleValidator.class)
    public void save(){
        Role role = getModel(Role.class, "");
        Ret ret = srv.save(role);
        renderJson(ret);
    }

    public void enterEdit(){
        int id = getInt(0);
        Role role = srv.getById(id);
        setAttr("role", role);
        render("respRoleEdit.html");
    }

    @Before(RoleValidator.class)
    public void update(){
        Role role = getModel(Role.class, "");
        Ret ret = srv.update(role);
        renderJson(ret);
    }

    public void delete(){

    }

    public void updateStruts(){
        int id = getInt("id");
        boolean flag = getBoolean("status");
        Ret ret = srv.updateStruts(id, flag);

        renderJson(ret);
    }

    /**
     * 批量禁用
     */
    public void batchRemove(){
        String ids = get(0);
        Ret ret = srv.batchRemove(ids);
        renderJson(ret);
    }
}
